package com.hongyuan.sellfitness.ui.person.view_mode;

import android.app.Application;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.hongyuan.mvvmhabitx.binding.command.BindingAction;
import com.hongyuan.mvvmhabitx.binding.command.BindingCommand;
import com.hongyuan.mvvmhabitx.bus.event.SingleLiveEvent;
import com.hongyuan.sellfitness.BR;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomViewModel;
import com.hongyuan.sellfitness.data.MyRepository;
import com.hongyuan.sellfitness.ui.home.activity.CoachDetailsActivity;
import com.hongyuan.sellfitness.ui.home.beans.CoachidleTimeContentBean;
import com.hongyuan.sellfitness.ui.home.beans.GroupClassTopBean;
import com.hongyuan.sellfitness.ui.main.activity.SuccessActivity;
import com.hongyuan.sellfitness.ui.main.beans.V3SuccessBeans;
import com.hongyuan.sellfitness.ui.person.beans.AddReservationLessonBean;
import com.hongyuan.sellfitness.ui.person.beans.ReservationLessonBean;

import java.util.List;

import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class ReservationLessonViewModel extends CustomViewModel<MyRepository> {

    public ObservableField<ReservationLessonBean> entity = new ObservableField<>();

    public ReservationLessonViewModel(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //给RecyclerView添加ObservableList
    public ObservableList<FansItemViewModel> observableList = new ObservableArrayList<>();

    //给RecyclerView添加ItemBinding
    public ItemBinding<FansItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_fans);

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        //设置弹框头部数据
        public SingleLiveEvent<List<GroupClassTopBean>> setTimeTopData = new SingleLiveEvent<>();
        //设置弹框内容数据
        public SingleLiveEvent<List<CoachidleTimeContentBean>> setTimeContentData = new SingleLiveEvent<>();
        //预约课程
        public SingleLiveEvent<Void> reservation = new SingleLiveEvent<>();
    }

    //拨打电话
    public BindingCommand callTel = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            getUC().getCallTel().setValue(entity.get().getM_mobile());
        }
    });

    //教练主页
    public BindingCommand goCoachPage = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Bundle bundle = new Bundle();
            bundle.putString("coach_mid",String.valueOf(entity.get().getM_id()));
            startActivity(CoachDetailsActivity.class,bundle);
        }
    });

    //预约
    public BindingCommand reservation = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            uc.reservation.call();
        }
    });

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.GET_RESERVATION_LESSON_INFO){
            ReservationLessonBean lessonBean = (ReservationLessonBean)dataBean;
            entity.set(lessonBean);

            //请求时间表格头部数据
           setParams("jl_mid",String.valueOf(lessonBean.getM_id()));
            requestData(Constants.GET_PLAN_DATE);
        }

        if(code == Constants.GET_PLAN_DATE){
            List<GroupClassTopBean>  mList = (List<GroupClassTopBean>) dataBean;
            uc.setTimeTopData.setValue(mList);
        }

        if(code == Constants.GET_COACH_TIMEPLAN_DAY_LIST){
            List<CoachidleTimeContentBean> list = (List<CoachidleTimeContentBean>) dataBean;
            uc.setTimeContentData.setValue(list);
        }

        if(code == Constants.ADD_COURSE_PRIVITE_APPOINTMENT){
            AddReservationLessonBean lessonBean = (AddReservationLessonBean)dataBean;

            V3SuccessBeans beans = new V3SuccessBeans();
            beans.setType(V3SuccessBeans.TYPE.PRIVITECLASS);
            beans.setTitleText("预约私教课");
            beans.setShowText("预约成功");
            beans.setBtn2Text("返回");

            Bundle bundle = new Bundle();
            bundle.putSerializable("successBeans",beans);
            startActivity(SuccessActivity.class,bundle);
            finish();
        }

    }

}
